﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Hr.DAL
{
    /// <summary>
    /// 数据访问类Hr_Struct_DepartTreeRelation。
    /// </summary>
    public class Hr_Struct_DepartTreeRelation
    {
        public Hr_Struct_DepartTreeRelation()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool HasChildDepart(Guid ParentId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Hr_Struct_DepartTreeRelation where ParentId=@ParentId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ParentId", DbType.Guid, ParentId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Hr.Entity.Hr_Struct_DepartTreeRelation model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Hr_Struct_DepartTreeRelation(");
            strSql.Append("RecordId,ParentId,ChildId)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@ParentId,@ChildId)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "ParentId", DbType.Guid, model.ParentId);
            db.AddInParameter(dbCommand, "ChildId", DbType.Guid, model.ChildId);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Hr.Entity.Hr_Struct_DepartTreeRelation model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Hr_Struct_DepartTreeRelation set ");
            strSql.Append("ParentId=@ParentId,");
            strSql.Append("ChildId=@ChildId");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "ParentId", DbType.Guid, model.ParentId);
            db.AddInParameter(dbCommand, "ChildId", DbType.Guid, model.ChildId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Hr_Struct_DepartTreeRelation ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除树结构中的数据
        /// </summary>
        public static void DeleteByChild(Guid ChildDepartId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Hr_Struct_DepartTreeRelation ");
            strSql.Append(" where ChildId=@ChildId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ChildId", DbType.Guid, ChildDepartId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Hr_Struct_DepartTreeRelation GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ParentId,ChildId from Hr_Struct_DepartTreeRelation ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Hr.Entity.Hr_Struct_DepartTreeRelation model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ParentId,ChildId,(select DepartName from Hr_DepartmentInfo where ParentId = Hr_DepartmentInfo.DepartId) As ParentName,(select DepartProperty from Hr_DepartmentInfo where ParentId = Hr_DepartmentInfo.DepartId) as ParentProperty,(select DepartName from Hr_DepartmentInfo where ChildId = Hr_DepartmentInfo.DepartId) As ChildName,(select DepartProperty from Hr_DepartmentInfo where ChildId = Hr_DepartmentInfo.DepartId) as ChildProperty,(select DepartCode from Hr_DepartmentInfo where ChildId = Hr_DepartmentInfo.DepartId) As ChildCode ");
            strSql.Append(" FROM Hr_Struct_DepartTreeRelation ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "Hr_Struct_DepartTreeRelation");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Hr.Entity.Hr_Struct_DepartTreeRelation> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ParentId,ChildId,(select DepartName from Hr_DepartmentInfo where ParentId = Hr_DepartmentInfo.DepartId) As ParentName,(select DepartProperty from Hr_DepartmentInfo where ParentId = Hr_DepartmentInfo.DepartId) as ParentProperty,(select DepartName from Hr_DepartmentInfo where ChildId = Hr_DepartmentInfo.DepartId) As ChildName,(select DepartProperty from Hr_DepartmentInfo where ChildId = Hr_DepartmentInfo.DepartId) as ChildProperty ");
            strSql.Append(" FROM Hr_Struct_DepartTreeRelation ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Hr.Entity.Hr_Struct_DepartTreeRelation> list = new List<Chenxin.Lib.Hr.Entity.Hr_Struct_DepartTreeRelation>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Hr_Struct_DepartTreeRelation ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Hr.Entity.Hr_Struct_DepartTreeRelation model = new Chenxin.Lib.Hr.Entity.Hr_Struct_DepartTreeRelation();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["ParentId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ParentId = new Guid(ojb.ToString());
            }
            ojb = dataReader["ChildId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ChildId = new Guid(ojb.ToString());
            }
            ojb = dataReader["ParentName"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ParentName = ojb.ToString();
            }
            ojb = dataReader["ChildName"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ChildName = ojb.ToString();
            }
            ojb = dataReader["ParentProperty"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ParentProperty = (int)ojb;
            }
            ojb = dataReader["ChildProperty"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ChildProperty = (int)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}

